from adapters.BaseAdapter import BaseAdapter
from google.appengine.api.urlfetch import fetch
from model import UserRecord
from model import UserSnapshot
import re

class TimusAdapter(BaseAdapter):

    def update_user(self, userRecord):
        data = fetch('http://acm.timus.ru/author.aspx?id=' + userRecord.timus_user_id + '&locale=en')
        snapshot = UserSnapshot( parent = userRecord, server = self.get_server() )

        res = re.search("solved</TD><TD CLASS=\"value\">[0-9]*", data)
        res = re.search("[0-9][0-9]*",res)
        res = int(res)
        snapshot.rating = res

        return snapshot

    def get_server(self):
        return "timus"

    def get_options(self):
        return dict( timus_user_id = "Timus User ID" )
